"""
题目：
从以下链接提取“民族大学”范围内房子的出租信息。其中的总价、单价、面积要存储为数值而非文本，不需要排序。(文件名：链家租金.py)
地址：
https://bj.lianjia.com/zufang/rs%E6%B0%91%E6%97%8F%E5%A4%A7%E5%AD%A6/

找一个能唯一标识的房屋信息：
整租·民族大学家属院 3室0厅 东/北

显示结果、
要去掉一些多余的字符，如：空格、回车等，使结果显得紧凑、顺眼。
"""

# -*- coding:utf8 -*-

import urllib.request
from bs4 import BeautifulSoup
import re

ranges = [
  {"from": ord(u"\u3300"), "to": ord(u"\u33ff")},         # compatibility ideographs
  {"from": ord(u"\ufe30"), "to": ord(u"\ufe4f")},         # compatibility ideographs
  {"from": ord(u"\uf900"), "to": ord(u"\ufaff")},         # compatibility ideographs
  {"from": ord(u"\U0002F800"), "to": ord(u"\U0002fa1f")}, # compatibility ideographs
  {'from': ord(u'\u3040'), 'to': ord(u'\u309f')},         # Japanese Hiragana
  {"from": ord(u"\u30a0"), "to": ord(u"\u30ff")},         # Japanese Katakana
  {"from": ord(u"\u2e80"), "to": ord(u"\u2eff")},         # cjk radicals supplement
  {"from": ord(u"\u4e00"), "to": ord(u"\u9fff")},
  {"from": ord(u"\u3400"), "to": ord(u"\u4dbf")},
  {"from": ord(u"\U00020000"), "to": ord(u"\U0002a6df")},
  {"from": ord(u"\U0002a700"), "to": ord(u"\U0002b73f")},
  {"from": ord(u"\U0002b740"), "to": ord(u"\U0002b81f")},
  {"from": ord(u"\U0002b820"), "to": ord(u"\U0002ceaf")}  # included as of Unicode 8.0
]

def is_cjk(char):
  return any([range["from"] <= ord(char) <= range["to"] for range in ranges])

def get_width(str_):
    cnt=0
    for c in str_:
        # if c >= u'\u0030' and c <= u'\u0039': # 汉字 全角 占二个空格计算
        if is_cjk(c):
            cnt=cnt+2
        else:  # 数字、字母
            cnt=cnt+1
    return cnt

def printTable(title,list_content):
    table = [title]+list_content
    cols=len(title)  # 列
    rows=len(table)  # 行

    width_of_table_element=[[get_width(s) for s in l ] for l in table]  # 生成二维列表 每个元素的宽度
    width_each_col=[max(c_line[i] for c_line in width_of_table_element ) for i in range(cols)]  # 每一列的最大宽度

    for i in range(rows):
        for j in range(cols):
            print(table[i][j] ,end=' '*(width_each_col[j]-width_of_table_element[i][j]+1))  # 以每一列最大宽度为标准，不足的用" "补充
        print()

List_HouseAll  = []         #总表，所有房屋信息。二维列表。

# 链接地址解析-------------------------<链家房价>------------------------------------------------
url = "https://bj.lianjia.com/zufang/rs%E6%B0%91%E6%97%8F%E5%A4%A7%E5%AD%A6/"
html = urllib.request.urlopen(url)
soup = BeautifulSoup(html.read().decode('utf-8'), "lxml")

# 找到【第1层标签】-------------------------------------------------------------------------
tag1_all = soup.find_all( "div", {"class":"content__list--item"} )
                                    # 单个或多个属性都可用字典形式,{"class":"item" ,"data-houseid":"101111735457"}
                                    # find_all返回满足条件的【所有】结果
                                    # find返回满足条件的【第1个】结果

title = ["名字","位置","面积","朝向","户型","楼层","特色","租金","单价(每平方米价格)"]
# 遍历find_all找到的所有结果-------------------------------------------------------------------------
for tag1_InfoClear in tag1_all:
    #【第2层标签】：获取房屋名称信息-------------------------------------------------------------------------
    tag2_title=tag1_InfoClear.find("p",class_="content__list--item--title")
    # print("房屋名称：", tag2_title.text )            #如果是tag2_title.text，则结果不同
    str_title=tag2_title.text
    # 对提取的标题处理
    str_title=str_title.replace("\n","")
    str_title=str_title.replace("\t","")
    str_title=str_title.strip() # 去掉前后空格
    # str_title=str_title.replace(" ","")
    # print("#{",str_title,"}#")

    #【第2层标签】：获取房屋位置、面积、楼层等信息-------------------------------------------------------------------------
    tag2_des=tag1_InfoClear.find("p", class_="content__list--item--des")             #找标签flood、positionInfo都可以

    str_des=str(tag2_des.text)
    str_des=str_des.replace("\n","")
    str_des=str_des.replace("\t","")
    # str_des=str_des.strip()   # 效果欠佳
    str_des=str_des.replace(" ","")
    # print("{",str_des,"}")
    list_str_des=str_des.split("/")
    # print("列表:",list_str_des)
    mianji=float(list_str_des[1][:-1])  # 得到浮点数 面积


    # 第二层标签 --- 获取房屋特色信息
    tag2_feature=tag1_InfoClear.find("p",class_="content__list--item--bottom oneline")
    # str_feature= str(tag2_feature.text)
    # str_feature=str_feature.replace("\n","")
    # str_feature=str_feature.replace("\t","")
    # print(str_feature)

    str_feature=""
    for feature in tag2_feature:
        str_feature=str_feature+feature.text+" "
    str_feature = str_feature.replace("\n", "")
    str_feature=str_feature.replace("\t","")
    # print(str_feature)

    # 获取总价
    tag2_price=tag1_InfoClear.find("span","content__list--item-price")
    str_price=str(tag2_price.text)
    price=float(str_price[:-4])
    # print(str_price)

    # 单价
    unit_price=price/mianji
    # 保留二位小数
    unit_price=round(unit_price,2)

    # print(unit_price)


    #存入列表-------------------------------------------------------------------------
    List_HouseTemp=[]                           #临时变量，存每一个房屋信息，再追加至List_HouseAll中
    List_HouseTemp.append(str_title)
    # List_HouseTemp.append(list_str_des)

    for i in list_str_des:
        List_HouseTemp.append(i)
    List_HouseTemp.append(str_feature)
    List_HouseTemp.append(str_price)
    List_HouseTemp.append(str(unit_price)+"元/㎡")
    # 把当前房屋信息追加至总表中
    List_HouseAll.append(List_HouseTemp)



# 调整打印方式
# 很显然 不是这么好调整格式的。。。。
printTable(title,List_HouseAll)


# quit()
# print(len(title))
# print(len(List_HouseAll[0]))
# print(title)
# print(List_HouseAll[0])
# quit()
# for i in title:
#     # print('{0:chr(12288) <15}'.format(i), end='')  # 沒能成功识别中文空格 chr(12288)
#     print(i.ljust(25,chr(12288)),end='')
# print('\n')
#
# for line in List_HouseAll:
#     for i in range(len(line)):
#         # if i<3:
#         #     print(line[i].ljust(10,chr(12288)),end='')
#         # else:
#             print(line[i].ljust(30),end='')
#     print('\n')
#
# quit()
# #显示二维列表---
# # ----------------------------------------------------------------------
# for house0 in List_HouseAll:
#     print(house0)



